package com.aaa.system.service.impl;

import com.aaa.entity.Notice;
import com.aaa.entity.User;
import com.aaa.system.dao.NoticeDao;
import com.aaa.system.dao.UserDao;
import com.aaa.system.service.INoticeService;
import com.aaa.utils.JWTUtils;
import com.aaa.utils.WebUtil;
import com.aaa.vo.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 * 通知公告表 服务实现类
 * </p>
 *
 * @author L
 * @since 2023-03-24
 */
@Service
public class NoticeServiceImpl extends ServiceImpl<NoticeDao, Notice> implements INoticeService {

    @Autowired
    private NoticeDao noticeDao;

    @Autowired
    private UserDao userDao;

    @Override
    public Result<IPage<Notice>> selectAll(Integer current, Integer size, Notice notice) {
        IPage<Notice> page = new Page<>(current,size);
        QueryWrapper<Notice> queryWrapper = new QueryWrapper<>();
        if (StringUtils.hasText(notice.getNoticeTitle())){
            queryWrapper.like("notice_title",notice.getNoticeTitle());
        }
        if (StringUtils.hasText(notice.getCreateBy())){
            queryWrapper.like("create_by",notice.getCreateBy());
        }
        if (Objects.nonNull(notice.getNoticeType())){
            queryWrapper.eq("notice_type",notice.getNoticeType());
        }
        if (Objects.nonNull(notice.getStatus())){
            queryWrapper.eq("status",notice.getStatus());
        }
        IPage<Notice> noticeIPage = noticeDao.selectPage(page,queryWrapper);
        return new Result<>(200,"查询所有通知公告成功",noticeIPage);
    }

    @Override
    public Result<Boolean> saveOrUpdateNotice(Notice notice) {
        //直接在token获取手机号
        HttpServletRequest request = WebUtil.getRequest();
        String token = request.getHeader("token");
        Map<String, Object> tokenInfo = JWTUtils.getInfo(token);
        String phone = tokenInfo.get("username").toString();
        //通过token获取登录用户信息
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phone",phone);
        User userByToken = userDao.selectOne(queryWrapper);

        if (Objects.nonNull(notice.getNoticeId())){
            //如果有通知公告Id，那么就是修改操作
            notice.setUpdateTime(LocalDateTime.now());
            notice.setUpdateBy(userByToken.getUserName());
            int i = noticeDao.updateById(notice);
            return new Result<>(200,"修改通知公告成功",i>0?true:false);
        }else{
            //如果没有通知公告Id，那么就是添加操作
            notice.setCreateTime(LocalDateTime.now());
            notice.setCreateBy(userByToken.getUserName());
            int insert = noticeDao.insert(notice);
            return new Result<>(200,"添加通知公告成功",insert>0?true:false);
        }
    }

    @Override
    public Result<Boolean> deleteNoticeByIds(Long[] noticeIds) {
        Boolean b = null;
        if (noticeIds.length>0){
            for (Long noticeId : noticeIds) {
                noticeDao.deleteById(noticeId);
            }
            b = true;
        }
        return new Result<Boolean>(200,"删除成功",b);
    }
}
